#第十章 采集JavaScript

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

import time

#通过selenium和PhantomJS采集通过js生成的网页内容
driver = webdriver.PhantomJS(executable_path="phantomjs")

driver.get("http://pythonscraping.com/pages/javascript/ajaxDemo.html")

try:
    #通过检查html元素变更来判断 是否生成了想要的html内容
    element = WebDriverWait(driver,10).until(EC.presence_of_element_located((By.ID,'loadedButton')))
finally:
    print(driver.find_element_by_id('content').text)
    driver.close()

#time.sleep(3)
